package com.xbongbong.pro.service.toolbox.data;

import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.help.FormDataHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.model.PaasFormDataModel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/07/15 10:25
 */
@Component
public class ProFormDataHelp {

    @Resource
    private PaasFormDataModel paasFormDataModel;
    @Resource
    private FormDataHelp formDataHelp;

    public PaasFormDataEntity getFormData(String corpid, Integer saasMark, Integer businessType, Long dataId) throws XbbException {
        PaasFormDataEntity paasFormDataEntity = null;
        if (Objects.equals(saasMark, SaasMarkEnum.PAAS.getCode())) {
            paasFormDataEntity = paasFormDataModel.getByKey(dataId, corpid);
        } else {
            PaasFormDataEntityExt paasFormDataEntityExt = formDataHelp.getFormData(dataId, corpid, businessType);
            if (paasFormDataEntityExt != null) {
                paasFormDataEntity = paasFormDataEntityExt;
            }
        }
        if (paasFormDataEntity == null) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_DATA_NOT_EXIST);
        }
        return paasFormDataEntity;
    }

    public PaasFormDataEntityExt getFormDataExt(String corpid, Integer saasMark, Integer businessType, Long dataId) throws XbbException {
        PaasFormDataEntityExt paasFormDataEntity;
        if (Objects.equals(saasMark, SaasMarkEnum.PAAS.getCode())) {
            paasFormDataEntity = paasFormDataModel.getByKey(dataId, corpid);
        } else {
            paasFormDataEntity = formDataHelp.getFormData(dataId, corpid, businessType);
        }
        if (paasFormDataEntity == null) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_DATA_NOT_EXIST);
        }
        return paasFormDataEntity;
    }
}
